package com.aidong.web.controller.dic;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.DicAdmAreaCodeDao;
import com.aidong.dao.wills.DicShopCodeDao;
import com.aidong.dao.wills.SysUserDao;
import com.aidong.model.wills.DicAdmAreaCode;
import com.aidong.model.wills.DicShopCode;
import com.aidong.web.controller.BaseController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"dicAdmArea"})
public class DicAdmAreaController extends BaseController {
    @Resource
    DicAdmAreaCodeDao dicAdmAreaCodeDao;
    @Resource
    DicShopCodeDao dicShopCodeDao;
    @Resource
    SysUserDao sysUserDao;

    public DicAdmAreaController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("dic/admarea/list")).addObject("areaType", this.getTypeList("admAreaType"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "areaType",required = false) String areaType, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        cd.eq("areaType", areaType);
        ConditionMap cd1 = new ConditionMap(1);
        if(!this.chkNull(name)) {
            cd1.like("code", "%" + name + "%");
            cd1.like("chName", "%" + name + "%");
            cd.addSub(cd1);
        }

        this.dicAdmAreaCodeDao.queryPageList(pg, cd, (new OrderSet()).asc("code"));
        ModelAndView mv = new ModelAndView("dic/admarea/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        DicAdmAreaCode area = new DicAdmAreaCode();
        List shopList = this.dicShopCodeDao.getAllShopList();
        Iterator mv = shopList.iterator();

        while(mv.hasNext()) {
            DicShopCode shop = (DicShopCode)mv.next();
            shop.setStatus(Integer.valueOf(0));
        }

        ModelAndView mv1 = new ModelAndView("dic/admarea/edit");
        mv1.addObject("area", area);
        mv1.addObject("shopList", shopList);
        mv1.addObject("areaType", this.getTypeList("admAreaType"));
        mv1.addObject("userList", this.sysUserDao.getListByGroup(new Object[]{"G102", "G111", "G121", "G141"}));
        mv1.addObject("_oper", "add");
        return mv1;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("area") DicAdmAreaCode area, @RequestParam(value = "shops",required = false) String shops) {
        if(this.chkNull(area.getCode())) {
            return this.getResult(false, "请输入编号");
        } else if(this.chkNull(area.getChName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.dicAdmAreaCodeDao.exists(area.getCode())) {
            return this.getResult(false, "编号已存在");
        } else {
            area.setCreateUser(this.getLoginUserCode());
            area.setCreateTime(new Date());
            area.setUpdateTime(new Date());
            ArrayList shopList = new ArrayList();
            if(!this.chkNull(shops)) {
                String[] e = shops.split(",");
                String[] arr$ = e;
                int len$ = e.length;

                for(int i$ = 0; i$ < len$; ++i$) {
                    String s = arr$[i$];
                    if(!this.chkNull(s)) {
                        shopList.add(s.trim());
                    }
                }
            }

            try {
                this.dicAdmAreaCodeDao.add(area);
                this.dicAdmAreaCodeDao.updateAreaShop(area.getCode(), shopList);
                this.reloadTypeList("admArea");
                this.addUserLog("新增门店管理区域", "编号:" + area.getCode() + ",名称:" + area.getChName());
            } catch (Exception var9) {
                this.log.error("add admarea", var9);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "保存完成");
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("code") String code) {
        DicAdmAreaCode area = (DicAdmAreaCode)this.dicAdmAreaCodeDao.getById(code);
        if(area == null) {
            return this.showAlert("没有这个记录");
        } else {
            List shopList = this.dicAdmAreaCodeDao.getAreaShopList(code, true);
            ModelAndView mv = new ModelAndView("dic/admarea/edit");
            mv.addObject("area", area);
            mv.addObject("shopList", shopList);
            mv.addObject("areaType", this.getTypeList("admAreaType"));
            mv.addObject("userList", this.sysUserDao.getListByGroup(new Object[]{"G102", "G111", "G121", "G141"}));
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("area") DicAdmAreaCode area, @RequestParam(value = "shops",required = false) String shops) {
        if(this.chkNull(area.getCode())) {
            return this.getResult(false, "请输入编号");
        } else if(this.chkNull(area.getChName())) {
            return this.getResult(false, "请输入名称");
        } else {
            DicAdmAreaCode area1 = (DicAdmAreaCode)this.dicAdmAreaCodeDao.getById(area.getCode());
            if(area1 == null) {
                return this.getResult(false, "没有这个记录");
            } else {
                area1.setChName(area.getChName());
                area1.setAreaType(area.getAreaType());
                area1.setBeginTime(area.getBeginTime());
                area1.setEndTime(area.getEndTime());
                area1.setChargePerson(area.getChargePerson());
                area1.setNote(area.getNote());
                area1.setUpdateTime(new Date());
                ArrayList shopList = new ArrayList();
                if(!this.chkNull(shops)) {
                    String[] e = shops.split(",");
                    String[] arr$ = e;
                    int len$ = e.length;

                    for(int i$ = 0; i$ < len$; ++i$) {
                        String s = arr$[i$];
                        if(!this.chkNull(s)) {
                            shopList.add(s.trim());
                        }
                    }
                }

                try {
                    this.dicAdmAreaCodeDao.update(area1);
                    this.dicAdmAreaCodeDao.updateAreaShop(area.getCode(), shopList);
                    this.reloadTypeList("admArea");
                    this.addUserLog("修改门店管理区域", "编号:" + area.getCode() + ",名称:" + area.getChName());
                } catch (Exception var10) {
                    this.log.error("edit admarea", var10);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("code") String code) {
        DicAdmAreaCode area = (DicAdmAreaCode)this.dicAdmAreaCodeDao.getById(code);
        if(area == null) {
            return this.getResult(false, "没有这个记录");
        } else {
            try {
                this.dicAdmAreaCodeDao.delete(area);
                this.reloadTypeList("admArea");
                this.addUserLog("删除门店管理区域", "编号:" + area.getCode() + ",名称:" + area.getChName());
            } catch (Exception var4) {
                this.log.error("del admarea", var4);
                return this.getResult(false, "删除发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }
}
